//部分控制器
var app = angular.module('myApp.controllers');

//店铺详情
app.controller('shopDetail', ["$scope", "utilService", "$rootScope", "$ionicScrollDelegate", function ($scope, utilService, $rootScope, $ionicScrollDelegate) {

    // 是否收藏
    $scope.shopDetail = {
        isCollect: true,
        isFocus: true,
    }
    
    // tab切换 如果需要无限加载加这个 不需要就算了。
    $scope.integralParams = {
        limit: 10,
        page: -1
    }
    // 当前参数
    $scope.data = {
        id: "", //当前选中tab的ID
        top: 0,//当前选中tab滚动的位置
        isLoad: false,//当前选中tab是否是第一次load
        currPage: 0,//当前选中tab当前第几页
        hasMore: true,
        posts: [],//当前选中tab的数据
    };
    $scope.tabs = ["服务展示", "服务评价"];
    $scope.tabMap = {
        "服务展示": {
            currPage: -1,
            posts: [],
            isLoad: true,
            hasMore: true,
            id: "服务展示",
            top: 0
        },
        "服务评价": {
            currPage: -1,
            posts: [],
            isLoad: true,
            hasMore: true,
            id: "服务评价",
            top: 0
        }
    };
    $scope.isActive = function(item) {
        return item == $scope.data.id;
    }

    //标签切换
    $scope.switchTab = function (id) {
        var pos = $ionicScrollDelegate.getScrollPosition();
        pos && ($scope.data.top = pos.top);

        var map = $scope.tabMap[id];
        if (!map) {
            map = $scope.tabMap[id] = {};
        }

        $scope.data = map;
        $scope.integralParams.page = map.currPage;
        $ionicScrollDelegate.resize();
        !map.top && (map.top = 0);
        $ionicScrollDelegate.scrollTo(0, map.top || 0, false);
    };
    $scope.switchTab($scope.tabs[0]); //默认选择第一个

    // 点击收藏
    $scope.toggleCollect = function () {

        $scope.shopDetail.isCollect = !$scope.shopDetail.isCollect;
        if ($scope.shopDetail.isCollect) {
            $.toast("已收藏");
        } else {
            $.toast("取消收藏");
        }
    }
    
    // 操作关注
    $scope.showActionSheet = function () {
        // 显示操作表
        utilService.$ionicActionSheet.show({
            buttons: [
                // { text: '<span class="fs-14 color-base">继续关注</span>' },
                { text: '<span class="fs-14 color-red">取消关注</span>' },
            ],
            titleText: '<span class="fs-16 color-333">是否取消关注该店铺</span>',
            cancelText: '<span class="fs-14 color-333">取消</span>',
            buttonClicked: function(index) {
                if (index == 0) {
                    console.log('取消关注')
                    $.toast("已取消", "cancel");
                    $scope.shopDetail.isFocus = false;
                    return true;
                }
            }
        });
    }
    // 关注店铺
    $scope.focusShop = function () {
        $.toast("关注成功");
        $scope.shopDetail.isFocus = true;
    }
    $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '车主服务';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);
// 服务详情
app.controller('shopService', ["$scope", "utilService", "$rootScope", "$ionicScrollDelegate", function ($scope, utilService, $rootScope, $ionicScrollDelegate) {

    $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '车主服务';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);
// 购买服务
app.controller('shopBuy', ["$scope", "utilService", "$rootScope", "$ionicScrollDelegate", function ($scope, utilService, $rootScope, $ionicScrollDelegate) {

    $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '车主服务';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);
// 预约到店
app.controller('shopBook', ["$scope", "utilService", "$rootScope", "$ionicScrollDelegate", function ($scope, utilService, $rootScope, $ionicScrollDelegate) {

    $scope.submitData = {
        name: '',
        mobile: '',
        itemId: '',
        time: '',
        remark:''
    }
    // 模拟数据
    $scope.items = ['服务1','服务2','服务3','服务4','服务5','服务6','服务78']
    $("#bookItem").picker({
        title: "请选择预约项目",
        cols: [
            {
                textAlign: 'center',
                values:  $scope.items
            }
        ],
        onClose: function (res) {
            console.log(res.cols[0]['activeIndex'])
            console.log(res.value[0])
        }
    });
    var now = new Date();
    var today = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
    $("#bookTime").datetimePicker({
        min: today,
        onClose: function (res) {
            console.log(res)
        }
    });
    // 去预约
    $scope.toBook = function () {
        console.log($scope.submitData);
        utilService.$state.go('shop.success');
    }
    $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '预约到店';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);
// 预约成功
app.controller('shopSuccess', ["$scope", "utilService", "$rootScope", "$ionicScrollDelegate", function ($scope, utilService, $rootScope, $ionicScrollDelegate) {

    $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '预约到店';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);

